/*
 * PMComponent.java
 *
 * Created on February 1, 2008, 2:32 PM
 */
package jdiskhog;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.RenderingHints;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JComponent;
import javax.swing.Timer;

/**
 *
 * @author  jbf
 */
public class PMComponent extends javax.swing.JPanel implements ProgressMonitor {

    /** Creates new form PMComponent */
    public PMComponent() {
        initComponents();
        jPanel1.add(new JComponent() {

            public static final int ROUNDNESS = 10;

            @Override
            public void paintComponent(Graphics g1) {
                Graphics2D g = (Graphics2D) g1;
                g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
                Rectangle b = getBounds();
                if (started) {
                    if (taskSize == -1) {
                        long t = System.currentTimeMillis();
                        int phase;
                        if (t - tickleTime < 1000) {
                            lastDrawnTime = System.currentTimeMillis();
                        }
                        phase = (int) (lastDrawnTime % 2000 - 500) / 10;

                        int x = phase * b.width / 100;

                        g.setColor(Color.BLUE);
                        int x0 = Math.max(0, x - b.width / 4);
                        int x1 = Math.min(b.width - 1, x + b.width / 4);

                        g.fillRoundRect(x0, 0, x1 - x0, b.height - 1, ROUNDNESS, ROUNDNESS);
                        g.setColor(Color.BLACK);

                    } else {
                        int x = taskProgress * b.width / taskSize;
                        x = Math.min(b.width - 1, Math.max(x, 0));
                        g.setColor(Color.BLUE);
                        g.fillRoundRect(0, 0, x, b.height - 1, ROUNDNESS, ROUNDNESS);
                        g.setColor(Color.BLACK);

                    }
                }
                g.drawRoundRect(0, 0, b.width - 1, b.height - 1, ROUNDNESS, ROUNDNESS);
            }
        }, BorderLayout.CENTER);
        jPanel1.revalidate();
        ActionListener repaintActionListener = new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                repaint();
            }
        };
        timer = new Timer(30, repaintActionListener);
    }
    Timer timer;
    private int taskSize = -1;

    public void setTaskSize(int newtaskSize) {
        this.taskSize = newtaskSize;
        if (!timer.isRunning()) {
            timer.start();
        }
    }
    private long tickleTime = 0;
    private long lastDrawnTime = 0;  // last time the tickle time was recent, so we know where to stop
    private long startTime = System.currentTimeMillis();
    private int taskProgress = -1;

    public void setTaskProgress(int newtaskProgress) {
        this.taskProgress = newtaskProgress;
        tickleTime = System.currentTimeMillis();
        if (!timer.isRunning()) {
            timer.start();
        }
    }
    private String taskMessage = "";

    public void setTaskMessage(String newtaskMessage) {
        this.taskMessage = newtaskMessage;
        if ( newtaskMessage.length()>100 ) newtaskMessage= newtaskMessage.substring(0,100) + "...";
        messageLabel.setText(newtaskMessage);
    }
    boolean finished = true;

    public void finished() {
        finished = true;
        started = false;
        this.jButton1.setEnabled(false);
        this.messageLabel.setText("");
    }
    boolean started = false;

    public void started() {
        finished = false;
        started = true;
        this.jButton1.setEnabled(true);
    }
    private boolean cancelled = false;

    public boolean isCancelled() {
        return cancelled;
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        messageLabel = new javax.swing.JLabel();
        jButton1 = new javax.swing.JButton();

        setName("Form"); // NOI18N

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(jdiskhog.JDiskHogApp.class).getContext().getResourceMap(PMComponent.class);
        jPanel1.setBackground(resourceMap.getColor("jPanel1.background")); // NOI18N
        jPanel1.setName("jPanel1"); // NOI18N
        jPanel1.setOpaque(false);
        jPanel1.setLayout(new java.awt.BorderLayout());

        messageLabel.setFont(resourceMap.getFont("messageLabel.font")); // NOI18N
        messageLabel.setText(resourceMap.getString("messageLabel.text")); // NOI18N
        messageLabel.setName("messageLabel"); // NOI18N

        jButton1.setText(resourceMap.getString("jButton1.text")); // NOI18N
        jButton1.setEnabled(false);
        jButton1.setName("jButton1"); // NOI18N
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(messageLabel, javax.swing.GroupLayout.DEFAULT_SIZE, 284, Short.MAX_VALUE)
                    .addComponent(jPanel1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 284, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, 13, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(messageLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE))
            .addComponent(jButton1, javax.swing.GroupLayout.Alignment.TRAILING)
        );
    }// </editor-fold>//GEN-END:initComponents
    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        cancelled = true;
        this.jButton1.setEnabled(false);
        jButton1.setText("*");
    }//GEN-LAST:event_jButton1ActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    public javax.swing.JButton jButton1;
    public javax.swing.JPanel jPanel1;
    public javax.swing.JLabel messageLabel;
    // End of variables declaration//GEN-END:variables
}
